﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class From2 : Form
    {
        public From2()
        {
            InitializeComponent();
        }

        private void From2_Load(object sender, EventArgs e)
        {
            var cmdString = "select * from Articles";
            var dt = DebHelper.GetDataTable(cmdString);
            dataGridView1.DataSource = dt;
            dataGridView1.ReadOnly = true;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.AllowUserToAddRows = false;
        }
        
        
        /// 添加
        private void button2_Click(object sender, EventArgs e)
        {
            Editform editForm = new Editform();
            var res = editForm.ShowDialog();

                var name = textBox1.Text;
                var sql = string.Format("select * from Articles where Title like '%{0}%'", name);
                var dt = DebHelper.GetDataTable(sql);
                dataGridView1.DataSource = dt;
         
        }


        /// 查找
        private void button1_Click(object sender, EventArgs e)
        {
            var name = textBox1.Text;
            var select = string.Format("select * from Articles where Title like '%{0}%'", name);
            var dt = DebHelper.GetDataTable(select);
            dataGridView1.DataSource = dt;
       }
        
        
        /// 更新
        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
                var title = (string)dataGridView1.SelectedRows[0].Cells["Title"].Value;
                var author = (string)dataGridView1.SelectedRows[0].Cells["Author"].Value;
                var time = (string)dataGridView1.SelectedRows[0].Cells["CreatedTime"].Value;

                Editform editForm = new Editform(id, title, author, time);
                var res = editForm.ShowDialog();

                    var titleTemp =textBox1.Text;
                    var sql = string.Format("select * from Articles where Title like '%{0}%'", titleTemp);
                    var dt = DebHelper.GetDataTable(sql);
                    dataGridView1.DataSource = dt;

                if (res==DialogResult.Yes)
                {
                    MessageBox.Show("修改成功", "提示");

                }
                else
                {
              
                }

            }
            else
            {
                MessageBox.Show("大哥你要选择行数才能进行更新啊！！！！");
            }
        }
        
        
        /// 删除
        private void button4_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var row = dataGridView1.SelectedRows[0];

                var id = (int)row.Cells["Id"].Value;

                var res = MessageBox.Show("确定删除吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);


                if (res.Equals(DialogResult.Yes))
                {
                    var sql = string.Format("delete from Articles where Id={0}", id);
                    var rowCount = DebHelper.AddOrUpDateOrDelete(sql);
                    if (rowCount > 0)
                    {
                        var name = textBox1.Text;
                        var sqlStu = string.Format("select * from Articles where Title like '%{0}%'", name);
                        var dt = DebHelper.GetDataTable(sqlStu);

                        dataGridView1.DataSource = dt;


                        MessageBox.Show("删除成功", "提示");
                    }
                }
            }
            else
            {
                MessageBox.Show("没有选择任何的行", "提示");
            }
        }

        
    }
    }

            
 
